package com.xjcs.saplingplan.ui.activity.memo

import android.os.Bundle
import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.KeyboardUtils
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.adapter.MemoBgColorAdapter
import com.xjcs.saplingplan.adapter.MemoToolsAdapter
import com.xjcs.saplingplan.adapter.MemoTvColorAdapter
import com.xjcs.saplingplan.base.BaseVbActivity
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.databinding.ActivityMemoCreateBinding
import com.xjcs.saplingplan.model.bean.memo.MemoBean
import com.xjcs.saplingplan.model.bean.memo.MemoStyleBean
import com.xjcs.saplingplan.model.bean.memo.MemoStyleUtils
import com.xjcs.saplingplan.model.bean.memo.MemoToolsBean
import com.xjcs.saplingplan.view.SoftKeyBoardListener

class MemoCreateActivity : BaseVbActivity<ActivityMemoCreateBinding>() {

    private val memoBean = MemoBean()

    private var tvColorAdapter: MemoTvColorAdapter? = null
    private var bgColorAdapter: MemoBgColorAdapter? = null
    private var memoToolsAdapter: MemoToolsAdapter? = null
    private val toolsList = getMemoToolsList()

    private var curMemoStyleBean: MemoStyleBean = MemoStyleBean()

    private var curToolsPos = -1

    override fun getViewBinding(): ActivityMemoCreateBinding {
        return ActivityMemoCreateBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        initEditor()
        initRecyclerTools()
        initRecyclerColor()
        initRecyclerBgColor()
        KeyboardUtils.showSoftInput()
    }

    private fun initRecyclerTools() {
        mBinding.recyclerTools.layoutManager = GridLayoutManager(this, toolsList.size)
        memoToolsAdapter = MemoToolsAdapter(this, toolsList){
            curToolsPos = it
            when(it){
                0 -> {
                    controllerKeyboard()
                }
                1 -> {
                    controllerKeyboard()
                }
                2 -> {
                    curMemoStyleBean.isBold = !curMemoStyleBean.isBold
                    mBinding.editor.setBold()
                    toolsList[curToolsPos].selected = curMemoStyleBean.isBold
                    memoToolsAdapter?.notifyDataSetChanged()
                }
                3 -> {
                    curMemoStyleBean.isItalic = !curMemoStyleBean.isItalic
                    mBinding.editor.setItalic()
                    toolsList[curToolsPos].selected = curMemoStyleBean.isItalic
                    memoToolsAdapter?.notifyDataSetChanged()
                }
                4 -> {
                    curMemoStyleBean.isUnderline = !curMemoStyleBean.isUnderline
                    mBinding.editor.setUnderline()
                    toolsList[curToolsPos].selected = curMemoStyleBean.isUnderline
                    memoToolsAdapter?.notifyDataSetChanged()
                }
                5 -> {
                    curMemoStyleBean.isStrikeThrough = !curMemoStyleBean.isStrikeThrough
                    mBinding.editor.setStrikeThrough()
                    toolsList[curToolsPos].selected = curMemoStyleBean.isStrikeThrough
                    memoToolsAdapter?.notifyDataSetChanged()
                }
            }
        }
        mBinding.recyclerTools.adapter = memoToolsAdapter
    }

    private fun initRecyclerColor() {
        mBinding.recyclerColor.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        tvColorAdapter = MemoTvColorAdapter(this, MemoStyleUtils.getMemoTextColorList())
        mBinding.recyclerColor.adapter = (tvColorAdapter)
    }

    private fun initRecyclerBgColor() {
        mBinding.recyclerBgColor.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        bgColorAdapter = MemoBgColorAdapter(this, MemoStyleUtils.getMemoBgColorList())
        mBinding.recyclerBgColor.adapter = (bgColorAdapter)
    }

    override fun registerObserver() {
    }

    override fun initListener() {
        SoftKeyBoardListener.setListener(this, object: SoftKeyBoardListener.OnSoftKeyBoardChangeListener{
            override fun keyBoardShow(height: Int) {
                curToolsPos = -1
                mBinding.ivMemoDown.visibility = VISIBLE
                mBinding.linearContainer.layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, height)
            }

            override fun keyBoardHide(height: Int) {
                if(curToolsPos !in 0..1){
                    mBinding.ivMemoDown.visibility = INVISIBLE
                    mBinding.linearContainer.layoutParams = LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, 0)
                }
            }

        })

        //取消
        mBinding.tvCancel.setSingleClick {
            finish()
        }
        //保存
        mBinding.tvSure.setSingleClick {

        }
    }

    private fun initEditor() {
        mBinding.editor.setEditorFontSize(16)
        mBinding.editor.setPlaceholder("请输入内容...")
        mBinding.editor.setPadding(15, 15, 15, 15)
        mBinding.editor.focusEditor()
        mBinding.editor.setOnTextChangeListener {

        }
    }

    private fun controllerKeyboard() {
        if(KeyboardUtils.isSoftInputVisible(this)){
            KeyboardUtils.hideSoftInput(this)
            if(curToolsPos == 0){
                mBinding.recyclerTools.visibility = VISIBLE
            }
        }else{
            KeyboardUtils.showSoftInput()
        }
    }

    private fun getMemoToolsList(): MutableList<MemoToolsBean> {
        return mutableListOf<MemoToolsBean>().apply {
            add(MemoToolsBean(R.drawable.icon_memo_add))
            add(MemoToolsBean(R.drawable.icon_memo_a))
            add(MemoToolsBean(R.drawable.icon_memo_b))
            add(MemoToolsBean(R.drawable.icon_memo_i))
            add(MemoToolsBean(R.drawable.icon_memo_u))
            add(MemoToolsBean(R.drawable.icon_memo_s))
        }
    }

    override fun onStop() {
        super.onStop()

    }

}